<?php
	class MsAquisicaoDAO{
		var $db;

		function __construct($db){
			$this->db = $db;
		}

		function setDb($db){
			$this->db = $db;
		}

		function getDb(){
			return $this->db;
		}

		function addMsAquisicao($ma){
			$sql = "insert into ms_aquisicao
						(ms, vlr, dt_aquisicao, perc_frete, perc_ipi, perc_st, forn, nf)
					values
						({$ma->getMs()->getId()},'{$ma->getVlr()}','{$ma->getDtAquisicao()}','{$ma->getPercFrete()}','{$ma->getPercIpi()}','{$ma->getPercSt()}',".(is_object($ma->getForn()) ? $ma->getForn()->getId() : "null") .",'{$ma->getNf()}')";			
			$res = $this->getDb()->insert($sql);
			return $res;
		}

		function getLista($ms){
			$maLista = array();

			$sql = "select 
						MS_AQUISICAO,
						MS,
						DT_AQUISICAO,
						VLR,
						PERC_FRETE,
						VLR_FRETE,
						PERC_IPI,
						VLR_IPI,
						PERC_ST,
						VLR_ST,
						VLR_TOTAL_SEM_RET,
						VLR_TOTAL,
						FORN,
						NF
					from ms_aquisicao
					where ms = {$ms}
					order by DT_AQUISICAO desc";
			$res = $this->getDb()->query($sql);
			if($res){
				$mdao = new MsDAO($this->getDb());
				$fdao = new FornDAO($this->getDb());
				foreach($res as $r){
					$ms = $mdao->getMs($r['MS']);
					if($r['FORN']){
						$f = $fdao->getForn($r['FORN']);
					}
					else{
						$f = null;
					}
					$maLista[] = new MsAquisicao($r['MS_AQUISICAO'],$ms,$r['DT_AQUISICAO'],$r['VLR'],$r['PERC_FRETE'],$r['VLR_FRETE'],$r['PERC_ST'],$r['VLR_ST'],$r['PERC_IPI'],$r['VLR_IPI'],$r['VLR_TOTAL_SEM_RET'],$r['VLR_TOTAL'],$f,$r['NF']);					
				}
			}

			return $maLista;
		}

		function delMsAquisicao($ma){
			$sql = "delete from ms_aquisicao where ms_aquisicao = {$ma}";
			$res = $this->getDb()->delete($sql);
			return $res;
		}
	}
?>